﻿// <copyright file="View.cs" company="ThorIndustry">Copyright (c) ThorIndustry. All rights reserved.</copyright>

using System.Collections.Generic;

public class View : UnityEngine.MonoBehaviour, IGuiController
{
    private Dictionary<System.Type, Menu> menuByType = new Dictionary<System.Type, Menu>();
    private Menu currentMenu = null;
    private object[] lastParameters = null;

    public IViewService ViewService
    {
        get;
        set;
    }

    public Menu CurrentMenu
    {
        get
        {
            return this.currentMenu;
        }
    }

    public virtual void BackMenu()
    {
        // By default, when back is it, just hide the current menu.
        if (this.currentMenu != null)
        {
            this.currentMenu.Hide();
            this.currentMenu = null;
            this.lastParameters = null;
        }
    }

    public virtual void Initialize()
    {
        this.ViewService = Services.GetService<IViewService>();

        Menu[] menus = this.GetComponentsInChildren<Menu>();

        for (int index = 0; index < menus.Length; ++index)
        {
            this.menuByType.Add(menus[index].GetType(), menus[index]);
        }

        foreach (KeyValuePair<System.Type, Menu> kvp in this.menuByType)
        {
            kvp.Value.Initialize(this);
        }
    }

    public virtual void Hide()
    {
        foreach (KeyValuePair<System.Type, Menu> kvp in this.menuByType)
        {
            kvp.Value.Hide();
        }
    }
    
    public virtual void Show(params object[] parameters)
    {
        if (this.currentMenu != null)
        {
            this.currentMenu.Show(this.lastParameters);
        }
    }

    public virtual void ShowMenu(System.Type menuType, params object[] parameters)
    {
        if (this.currentMenu != null)
        {
            this.currentMenu.Hide();
            this.currentMenu = null;
            this.lastParameters = null;
        }

        if (this.menuByType.ContainsKey(menuType))
        {
            this.currentMenu = this.menuByType[menuType];
            this.lastParameters = parameters;

            this.currentMenu.Show(parameters);
        }
    }

    protected virtual void CameraConfiguration()
    {
    }
}
